package com.devin.typeHandler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeStampTypeHandler extends BaseTypeHandler<Date> {
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        preparedStatement.setString(i,format);
//        System.out.println("===========================");
    }

    @Override
    public Date getNullableResult(ResultSet resultSet, String s) throws SQLException {
        String string = resultSet.getString(s);    /*"2020-09-08 19:03:13.0"*/
        String s1 = string.split("\\.")[0];  /*删除.0*/
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse=null;
        try {
            parse = dateFormat.parse(s1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        System.out.println("getDate################");
        return parse;
    }

    @Override
    public Date getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return null;
    }

    @Override
    public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return null;
    }
}
